//基于vue3,axios,js,ant design vue封装的请求工具类
import axios from 'axios'
import { message } from 'ant-design-vue'
import useAppStore from '@/store/app'
import useUserStore from '@/store/user'
import systemLoading from '@/utils/loading'

export const http = axios.create({
  baseURL: '/api/v1/'
  // timeout: 999 * 1000 // 请求超时时间
})

//请求拦截器
http.interceptors.request.use((config) => {
  const appStore = useAppStore()
  const userStore = useUserStore()
  config.headers['content-type'] = 'application/json;charset=UTF-8'
  config.headers['Url'] = location.href || ''
  config.headers['Emp-Id'] = userStore.empId || ''
  config.headers['Last-Menu-Id'] = appStore.LAST_MENU_ID || ''
  config.headers['org-code'] = encodeURI(appStore.ORG_CODE || '')

  return config
})
//响应拦截器
http.interceptors.response.use(
  (response) => {
    if (response.data.code === 401 || response.data.code === 403) {
      const appStore = useAppStore()
      message.destroy()
      message.error(response.data.msg || '登录已失效，请重新登录！')
      appStore.loginOut(false)
      return { data: { data: null, code: -999, msg: '登录已失效，请重新登录！' } }
    } else {
      return response
    }
  },
  (error) => {
    if (error.code === 'ECONNABORTED' && error.message.indexOf('timeout') !== -1) {
      message.destroy()
      message.error('请求超时，请稍后重试！')
    } else {
      const { status } = error.response
      let errMessage = '未知错误'
      if (status) {
        switch (status) {
          case 400:
            errMessage = '请求错误'
            break
          case 401:
            errMessage = '未授权，请重新登录'
            break
          case 403:
            errMessage = '拒绝访问'
            break
          case 404:
            errMessage = '404,未找到该资源'
            break
          case 405:
            errMessage = '请求方法错误'
            break
          case 408:
            errMessage = '请求超时'
            break
          case 500:
            errMessage = '服务器端出错'
            break
          case 501:
            errMessage = '网络未实现'
            break
          case 502:
            errMessage = '网络错误'
            break
          case 503:
            errMessage = '服务不可用'
            break
          case 504:
            errMessage = '网络超时'
            break
          case 505:
            errMessage = 'http版本不支持该请求'
            break
          default:
            errMessage = `其他连接错误 --${status}`
        }
      } else {
        errMessage = `无法连接到服务器！`
      }
      message.destroy()
      message.error(errMessage)
    }
    return Promise.reject(error)
  }
)

export function get(url, params, options) {
  options?.loading && systemLoading(true)
  const resParams = getExtraParams(params)
  return new Promise((resolve, reject) => {
    http
      .get(url, { params: resParams })
      .then((res) => {
        options?.loading && systemLoading(false, 300)
        resolve(res?.data || {})
      })
      .catch((error) => {
        options?.loading && systemLoading(false)
        reject(error)
      })
  })
}
export function post(url, params, options) {
  options?.loading && systemLoading(true)
  const resParams = getExtraParams(params)
  return new Promise((resolve) => {
    http
      .post(url, resParams, options)
      .then((res) => {
        options?.loading && systemLoading(false, 300)
        res?.data.code !== -999 && resolve(res?.data)
      })
      .catch((error) => {
        options?.loading && systemLoading(false)
        error.code !== -999 && resolve({ code: -1, msg: '请求失败' })
      })
  })
}

const getExtraParams = (params) => {
  if (Array.isArray(params)) {
    return params
  }
  const appStore = useAppStore()
  const extraParams = params || {}
  //易耗品参数
  if (appStore.CURRENT_ROUTE_PATH === '/consumable-material') {
    extraParams.mtrType = 1
  }
  if (appStore.CURRENT_ROUTE_PATH === '/material') {
    extraParams.mtrType = 0
  }
  if (appStore.CURRENT_ROUTE_PATH === '/fixed-assets') {
    extraParams.mtrType = 2
  }
  return extraParams
}
